package com.hj.ct.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hj.ct.R;
import com.hj.ct.model.CompositeData;
import com.hj.ct.parse.GatherData;
import com.hj.ct.util.FileUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

public class DeviceListAdapter extends BaseAdapter {
	private Context mContext;
	private List<CompositeData> datas = new ArrayList<CompositeData>();

	public DeviceListAdapter(Context context) {
		this.mContext = context;
	}

	public void setDatas(List<CompositeData> datas) {
		this.datas = datas;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public CompositeData getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_device, null);
			viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_line1_01);
			viewHolder.tvNoise = (TextView) convertView.findViewById(R.id.tv_line2_01);
			viewHolder.tvDust = (TextView) convertView.findViewById(R.id.tv_line2_02);
			viewHolder.tvWindSpeed = (TextView) convertView.findViewById(R.id.tv_line2_03);
			viewHolder.tvWindDirection = (TextView) convertView.findViewById(R.id.tv_line2_04);
			viewHolder.imgAvatar = (ImageView) convertView.findViewById(R.id.img_avator);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		CompositeData data = getItem(position);
		GatherData gatherData = data.getGatherData();
		// 时间 粉尘浓度 噪声值 风速 风向
		viewHolder.tvName.setText(data.getDeviece().getDeviceName());
		String dust = gatherData == null ? "-" : gatherData.getDustInst();
		String noise = gatherData == null ? "-" : gatherData.getLAFMaxPrf();
		String windSpeed = gatherData == null ? "-" : gatherData.getWindSP();
		String windDirection = gatherData == null ? "-" : gatherData.getWindDir();
		viewHolder.tvDust.setText("-".equals(dust) ? dust : dust + "mg/m³");// 粉尘
		viewHolder.tvNoise.setText("-".equals(noise) ? noise : noise + "db");// 噪声
		viewHolder.tvWindSpeed.setText(windSpeed);// 风速
		viewHolder.tvWindDirection.setText(windDirection);// 风向
		loadGroupAvatar(data.getDeviece().getImg(), viewHolder.imgAvatar, R.drawable.icon_device_default);
		return convertView;
	}

	protected void loadGroupAvatar(String url, ImageView imgview, int defaultImageRes) {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(defaultImageRes).showImageForEmptyUri(defaultImageRes).showImageOnFail(defaultImageRes).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true).displayer(new SimpleBitmapDisplayer()).build();
		ImageLoader.getInstance().displayImage(url, imgview, options);
	}

	private class ViewHolder {
		TextView tvName;
		TextView tvNoise;
		TextView tvDust;
		TextView tvWindSpeed;
		TextView tvWindDirection;
		ImageView imgAvatar;
	}

}
